﻿using System;
using System.Collections.Generic;

namespace YArchitech.Plumbing
{
	public class HYPumpOperationCurveData : HYPumpOperationCurveDataBase
	{
		public HYPumpOperationCurveData()
		{
			this.m_pCurveData = new _InternalCurveData();
		}

		public HYPumpOperationCurveData(int nTimes) : base(nTimes)
		{
			this.m_pCurveData = new _InternalCurveData();
		}

		public void Clear()
		{
			this.m_pCurveData.Clear();
			this.m_ptArr.Clear();
			base.InitMatrix();
			base.InitParam();
		}

		public bool IsEmpty()
		{
			return this.m_pCurveData.Count <= 0;
		}

		public override bool GetMaxQandValue(ref double dQ, ref double dValue)
		{
			return this.m_pCurveData.Count > 0 && base.GetMaxQandValue(ref dQ, ref dValue);
		}

		public override bool GetMinQandValue(ref double dQ, ref double dValue)
		{
			return this.m_pCurveData.Count > 0 && base.GetMinQandValue(ref dQ, ref dValue);
		}

		public override bool GetMaxValue(ref double dValue, ref double dQ)
		{
			return this.m_pCurveData.Count > 0 && base.GetMaxValue(ref dValue, ref dQ);
		}

		public override bool GetMinValue(ref double dValue, ref double dQ)
		{
			return this.m_pCurveData.Count > 0 && base.GetMinValue(ref dValue, ref dQ);
		}

		public void GetSortedPtCurveArr(ref List<stCurvePt> ptArr)
		{
			if (this.m_pCurveData == null)
			{
				return;
			}
			if (ptArr == null)
			{
				ptArr = new List<stCurvePt>();
			}
			foreach (KeyValuePair<double, double> keyValuePair in this.m_pCurveData)
			{
				ptArr.Add(new stCurvePt(keyValuePair.Key, keyValuePair.Value));
			}
		}

		public void SetByPtCurveArr(ref List<stCurvePt> ptArr)
		{
			if (ptArr == null || ptArr.Count < 1)
			{
				return;
			}
			this.m_pCurveData.Clear();
			this.m_ptArr.Clear();
			foreach (stCurvePt stCurvePt in ptArr)
			{
				this.m_ptArr.Add(new stCurvePt(stCurvePt.first, stCurvePt.second));
			}
			foreach (stCurvePt stCurvePt2 in this.m_ptArr)
			{
				if (!this.m_pCurveData.ContainsKey(stCurvePt2.first))
				{
					this.m_pCurveData.Add(stCurvePt2.first, stCurvePt2.second);
				}
			}
			this.CalulateExtents();
		}

		public override void CalulateExtents()
		{
			if (this.m_pCurveData.Count <= 0 || this.m_ptArr.Count < 1)
			{
				return;
			}
			bool flag = true;
			foreach (KeyValuePair<double, double> keyValuePair in this.m_pCurveData)
			{
				if (flag)
				{
					this.m_curveLeftPt.first = keyValuePair.Key;
					this.m_curveLeftPt.second = keyValuePair.Value;
					flag = false;
				}
				this.m_curveRightPt.first = keyValuePair.Key;
				this.m_curveRightPt.second = keyValuePair.Value;
			}
			this.m_curveLowPt = this.m_ptArr[0];
			this.m_curveHighPt = this.m_ptArr[0];
			for (int i = 1; i < this.m_ptArr.Count; i++)
			{
				stCurvePt stCurvePt = this.m_ptArr[i];
				if (stCurvePt.second > this.m_curveHighPt.second)
				{
					this.m_curveHighPt = stCurvePt.New();
				}
				else if (stCurvePt.second < this.m_curveLowPt.second)
				{
					this.m_curveLowPt = stCurvePt.New();
				}
			}
		}

		public void FixExtentsByFitCurve()
		{
			if (this.m_curveLeftPt.first > 0.0)
			{
				this.FitCurveParam();
				this.InsertPt(0.0, base.CalCurveValue(0.0));
				this.CalulateExtents();
			}
		}

		public bool FitCurveParam()
		{
			if (this.m_ptArr.Count < this.m_nTimes + 1)
			{
				return false;
			}
			if (base.Calculation())
			{
				this.m_curveLeftPt.second = base.CalCurveValue(this.m_curveLeftPt.first);
				this.m_curveRightPt.second = base.CalCurveValue(this.m_curveRightPt.first);
				this.m_curveLowPt.second = base.CalCurveValue(this.m_curveLowPt.first);
				this.m_curveHighPt.second = base.CalCurveValue(this.m_curveHighPt.first);
				return true;
			}
			return false;
		}

		public void CreateCurveFitPoints(ref List<stCurvePt> ptArr, int nNums = 20)
		{
			double num = 0.0;
			double num2 = 0.0;
			bool flag = true;
			foreach (KeyValuePair<double, double> keyValuePair in this.m_pCurveData)
			{
				if (flag)
				{
					num = keyValuePair.Key;
					flag = false;
				}
				num2 = keyValuePair.Key;
			}
			double num3 = (num2 - num) / (double)nNums;
			for (int i = 0; i < nNums; i++)
			{
				double num4 = num + (double)i * num3;
				ptArr.Add(new stCurvePt(num4, base.CalCurveValue(num4)));
			}
		}

		private void InsertPt(double dQ, double dValue)
		{
			if (this.m_pCurveData.ContainsKey(dQ))
			{
				return;
			}
			this.m_pCurveData.Add(dQ, dValue);
			this.m_ptArr.Add(new stCurvePt(dQ, dValue));
		}

		private void RemovePt(double dQ)
		{
			this.m_pCurveData.Remove(dQ);
			foreach (stCurvePt stCurvePt in this.m_ptArr)
			{
				if (Math.Abs(stCurvePt.first - dQ) < 1E-07)
				{
					this.m_ptArr.Remove(stCurvePt);
					break;
				}
			}
		}

		protected _InternalCurveData m_pCurveData;
	}
}
